package com.ushine.web.base.util;

import com.ushine.web.base.model.base.BaseTreeBO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yls
 * @Date 2022/2/17 12:49
 * @Description: 树形菜单组装工具类
 * @Version 1.0
 **/
@Component
public class TreeAssemblyUtils {
    public List<? extends BaseTreeBO> getBaseTreeByGroup(List<? extends BaseTreeBO> baseTreeList) {
        Map<Long, List<BaseTreeBO>> baseTreeMap = new HashMap<>(10);
        baseTreeList.forEach(permissionDO -> {
            Long parentId = permissionDO.getParentId();
            //如果不存在该父id的分组，新建一个
            if (baseTreeMap.get(parentId) == null) {
                List<BaseTreeBO> tempBaseTreeList = new ArrayList<>();
                tempBaseTreeList.add(permissionDO);
                baseTreeMap.put(parentId, tempBaseTreeList);
                //如果已经存在，往里面添加数据即可
            } else {
                baseTreeMap.get(parentId).add(permissionDO);
            }
        });
        //组装tree
        //获取根节点数据
        List<BaseTreeBO> baseTree = baseTreeMap.get(0L);
        findChildren(baseTree, baseTreeMap);

        return baseTree;
    }


    /**
     * @param baseTreeList       菜单对应的tree
     * @param detGroupByParentId 菜单对应的根据父级id分组的map
     * @return void
     * @Author yls
     * @Description 组装部门tree
     * @Date 2020/4/6 13:29
     **/
    public void findChildren(List<BaseTreeBO> baseTreeList, Map<Long, List<BaseTreeBO>> detGroupByParentId) {
        if (baseTreeList == null || baseTreeList.isEmpty()) {
            return;
        }
        baseTreeList.forEach(baseTree -> {
            List<BaseTreeBO> childerns = detGroupByParentId.get(baseTree.getId());
            //判断是否有子部门
            if (childerns != null) {
                baseTree.setChildren(childerns);
                //递归扫描子级的children
                findChildren(childerns, detGroupByParentId);
            }
        });
    }
//
//    private List<PermissionDO> getPermissionsByGroup(List<PermissionDO> permissionDOList) {
//        Map<Long, List<PermissionDO>> parentPermissionMap = new HashMap<>(10);
//        permissionDOList.forEach(permissionDO -> {
//            Long parentId = permissionDO.getParentId();
//            //如果不存在该父id的分组，新建一个
//            if (parentPermissionMap.get(parentId) == null) {
//                List<PermissionDO> tempPermissionDOList = new ArrayList<>();
//                tempPermissionDOList.add(permissionDO);
//                parentPermissionMap.put(parentId, tempPermissionDOList);
//                //如果已经存在，往里面添加数据即可
//            } else {
//                parentPermissionMap.get(parentId).add(permissionDO);
//            }
//        });
//        //组装tree
//        //获取根节点数据
//        List<PermissionDO> permissionTree = parentPermissionMap.get(0L);
//        findChildren(permissionTree, parentPermissionMap);
//
//        return permissionTree;
//    }
//
//
//    /**
//     * @param permissionTree     菜单对应的tree
//     * @param detGroupByParentId 菜单对应的根据父级id分组的map
//     * @return void
//     * @Author yls
//     * @Description 组装部门tree
//     * @Date 2020/4/6 13:29
//     **/
//    private void findChildren(List<PermissionDO> permissionTree, Map<Long, List<PermissionDO>> detGroupByParentId) {
//        if (permissionTree == null || permissionTree.isEmpty()) {
//            return;
//        }
//        permissionTree.forEach(permissionDO -> {
//            List<PermissionDO> childerns = detGroupByParentId.get(permissionDO.getId());
//            //判断是否有子部门
//            if (childerns != null) {
//                permissionDO.setChildren(childerns);
//                //递归扫描子级的children
//                findChildren(childerns, detGroupByParentId);
//            }
//        });
//    }
}
